package net.vinote.smart.platform.module.database.customize;

import net.vinote.smart.platform.module.database.DataBaseFeature;

public class MySQL implements DataBaseFeature {

	public String getPgination(String querySql, int pageSize, int pageNum) {
		StringBuffer sb = new StringBuffer(querySql.trim());
		// 如果SQL语句以";"结尾,则将其 去除
		int lastIndex = sb.length() - 1;
		if (sb.lastIndexOf(";") == lastIndex) {
			sb.setCharAt(lastIndex, ' ');
		}
		if (pageNum > 0 && pageSize > 0) {
			return querySql + " LIMIT " + (pageNum - 1) * pageSize + ","
					+ pageSize;
		}
		return querySql;
	}

	public String getColumnSQL(String querySQL, String... columns) {
		StringBuffer sb = new StringBuffer("select ");
		int index = columns.length;
		for (String col : columns) {
			sb.append(col);
			if (--index > 0) {
				sb.append(",");
			}
		}
		sb.append(" from (").append(querySQL).append(") as TempColumnSQl");
		return sb.toString();
	}

}
